/*
* floyd O(n^3) 建立传递闭包
* 有向无权图
* 矛盾:
*      d[i][i] = 1 
*      d[i][j] == 1 && d[j][i] = 1
* 
*/
#pragma GCC optimize("O1,O2,O3,Ofast")
#pragma GCC optimize("no-stack-protector,unroll-loops,fast-math,inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,ssse3")

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
// #define ONLINE_GUDGE
using namespace std;
const int N = 30, M = N * N, INF = 0x3f3f3f3f;

int n, m;
bool g[N][N], d[N][N];
bool st[N]; 

// void AddEdge(int a, int b, int c)
// { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; }

void floyd()
{
    memcpy(d, g, sizeof g);

    for(int k = 0; k < n; k++)
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                d[i][j] |=  d[i][k] && d[k][j]; // i -> k -> j
}

int check()
{
    for(int i = 0; i < n; i++)
        if(d[i][i]) return 2; // 有矛盾

    for(int i = 0; i < n; i++)
        for(int j = 0; j < i; j++)
            if(!d[i][j] && !d[j][i]) return 0; // i,j不能确定关系
    
    return 1;
}

char GetMin()
{
    for(int i = 0; i < n; i++)
        if(!st[i])
        {
            bool flag = true;
            for(int j = 0; j < n; j++)
                if(!st[j] && d[j][i]) // j没有访问过 && j<i
                {
                    flag = false;
                    break;
                }
            if(flag)
            {
                st[i] = true;
                return 'A' + i;
            }
        }
    
    return 0;
        
}

int main()
{
    #ifdef ONLINE_JUDGE

    #else
        freopen("./in.txt", "r", stdin);
    #endif

    ios::sync_with_stdio(0); cin.tie(0);

    while(cin >> n >> m, n || m)
    {
        memset(g, 0, sizeof g);
        memset(d, 0, sizeof d);
        int type = 0, t; // 结果类型 结果经历轮数
        for(int i = 1; i <= m; i++)
        {
            char str[5]; cin >> str;
            int a = str[0] - 'A', b = str[2] - 'A';
            if(!type){ // 当前状态不确定
                // 用floyd 应该是g[a][b] = 1

                d[a][b] = 1;
                // floyd();
                // floyd 优化 为 O(n^2)
                 for(int i = 0; i < n; i++)
                    for(int j = 0; j < n; j++){
                        if(d[i][a] && d[b][j]) d[i][j] = 1; // i -> a -> b -> j
                        if(d[i][a]) d[i][b] = 1;
                        if(d[b][j]) d[a][j] = 1;
                    }

                type = check();
                if(type) t = i;
            }
        }

        /* 输出g[][]
        printf("  ");
        for(int i = 0; i < n; i++) printf("%c ", 'A' + i);
        printf("\n");
        for(int i = 0; i < n; i++)
        {
            printf("%c ", 'A' + i);
            for(int j = 0; j < n; j++) if(d[i][j]) printf("1 "); else printf("  ");// printf("%d ", d[i][j]);
            printf("\n");
        }
        */

        if(!type) printf("Sorted sequence cannot be determined.\n");
        else if(type == 2) printf("Inconsistency found after %d relations.\n", t);
        else{
            memset(st, 0, sizeof st);
            printf("Sorted sequence determined after %d relations: ", t);
            for(int i =0; i < n; i++)   printf("%c", GetMin());
            printf(".\n");
        }
    }
    return 0;
}